﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;
using Utility;
namespace Service
{
    /// <summary>
    /// 功能：Sys_Role类数据访问
    /// 创建人：Wilson
    /// 创建时间：2013-1-30   
    /// </summary>
    public class Sys_RoleService : IDisposable
    {
        #region 公共
        private System.ComponentModel.Component component = new System.ComponentModel.Component();

        private bool disposed = false;

        ~Sys_RoleService()
        {
            Dispose(false);
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!this.disposed)
            {

                if (disposing)
                {

                    component.Dispose();
                }

                disposed = true;

            }
        }
        #endregion
		#region##MapperSys_Role
        public IList<Sys_Role> MapperList(SqlDataReader reader)
         {
            IList<Sys_Role> list = new List<Sys_Role>();
            Sys_Role obj=null;
            while (reader.Read())
            {
                obj = new Sys_Role();
                obj.RoleId = (int)reader["RoleId"];
                obj.RoleName = (string)reader["RoleName"];
                obj.Sort = reader["Sort"] as int?;
                list.Add(obj);
            }
            return list;
         }
        public Sys_Role Mapper(SqlDataReader reader)
         {
            Sys_Role obj=null;
            if (reader.Read())
            {
                obj = new Sys_Role();
                obj.RoleId = (int)reader["RoleId"];
                obj.RoleName = (string)reader["RoleName"];
                    obj.Sort = reader["Sort"] as int?;
            }
            return obj;
         }
        #endregion
        #region##通过主键ID得到Sys_Role
        /// <summary>
        /// 功能：通过ID得到Sys_Role
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
        public Sys_Role GetById(int roleid)
        {            
            string sql = "select * from Sys_Role where RoleId = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",4)
            };
            
            SqlDataReader reader=SqlHelper.ExecuteReader(WebConn.connString,CommandType.Text,sql,sp);
            return Mapper(reader);
        }
        #endregion
		
		#region##通过主键ID删除Sys_Role
        /// <summary>
        /// 功能：通过主键ID删除Sys_Role
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(int roleid)
		{
			int i = 0;
			string sql = "delete from Sys_Role where RoleId = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",4)
            };
            i= SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
			return i;
		}
        /// <summary>
        /// 功能：通过主键ID删除Sys_Role
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(SqlTransaction transaction,int roleid)
		{
			int i = 0;
			string sql = "delete from Sys_Role where RoleId = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",4)
            };
            i= SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
			return i;
		}
		#endregion			
		
        #region##添加Sys_Role
        /// <summary>
        /// 功能：添加Sys_Role
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(Sys_Role sys_role)
        {
            int result = 0;
            string sql = "insert into Sys_Role(RoleId,RoleName,Sort) values(@RoleId,@RoleName,@Sort)";			
            SqlParameter[] sp = new SqlParameter[]
            {
				new SqlParameter("@RoleId",sys_role.RoleId),
				new SqlParameter("@RoleName",sys_role.RoleName),
				new SqlParameter("@Sort",sys_role.Sort)
            };
            result = SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
            return result;
        }
         /// <summary>
        /// 功能：添加Sys_Role
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(SqlTransaction transaction,Sys_Role sys_role)
        {
            int result = 0;
            string sql = "insert into Sys_Role(RoleId,RoleName,Sort) values(@RoleId,@RoleName,@Sort)";			
            SqlParameter[] sp = new SqlParameter[]
            {
				new SqlParameter("@RoleId",sys_role.RoleId),
				new SqlParameter("@RoleName",sys_role.RoleName),
				new SqlParameter("@Sort",sys_role.Sort)
            };
            result = SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
            return result;
        }
        #endregion
		
		#region##得到Sys_Role集合
		/// <summary>
        /// 功能：得到Sys_Role集合
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>       
        /// <returns></returns>
		public IList<Sys_Role> GetList()
		{
			string sql = "select * from Sys_Role";			
			SqlDataReader reader = SqlHelper.ExecuteReader(WebConn.connString,CommandType.Text,sql);
            return MapperList(reader);
		}
		#endregion
		
		#region##修改Sys_Role
        /// <summary>
        /// 功能：修改Sys_Role
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int ModSys_Role(Sys_Role sys_role)
        {
            int result = 0;
            string sql = "update Sys_Role set RoleId = @RoleId,RoleName = @RoleName,Sort = @Sort where RoleId = @RoleId";
            SqlParameter[] sp = new SqlParameter[]
            {       
				new SqlParameter("@RoleId",sys_role.RoleId),
				new SqlParameter("@RoleName",sys_role.RoleName),
				new SqlParameter("@Sort",sys_role.Sort)
            };
            result = SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
            return result;
        }
         /// <summary>
        /// 功能：修改Sys_Role
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int ModSys_Role(SqlTransaction transaction,Sys_Role sys_role)
        {
            int result = 0;
            string sql = "update Sys_Role set RoleId = @RoleId,RoleName = @RoleName,Sort = @Sort where RoleId = @RoleId";
            SqlParameter[] sp = new SqlParameter[]
            {       
				new SqlParameter("@RoleId",sys_role.RoleId),
				new SqlParameter("@RoleName",sys_role.RoleName),
				new SqlParameter("@Sort",sys_role.Sort)
            };
            result = SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
            return result;
        }
        #endregion
		
		#region##统计Sys_Role
		/// <summary>
        /// 修改Sys_Role
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
		public int SelectCount()
		{
			int i = 0;
            string sql = "select count(*) from Sys_Role";
            i = (int)SqlHelper.ExecuteScalar(WebConn.connString,CommandType.Text,sql);
            return i;
		}
		/// <summary>
        /// 修改Sys_Role
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int SelectCountById()
        {
            int i=0;
            string sql="select count(RoleId) from Sys_Role";
            i = (int)SqlHelper.ExecuteScalar(WebConn.connString,CommandType.Text,sql);
            return i;
        }
		#endregion
        #region##自定义Sys_Role
        #endregion
                
    }
}
